package com.campuscrowdsourcing.server.controller;


import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campuscrowdsourcing.server.annotation.Log;
import com.campuscrowdsourcing.server.annotation.LoginCheck;
import com.campuscrowdsourcing.server.common.enums.Action;
import com.campuscrowdsourcing.server.domain.entity.Task;
import com.campuscrowdsourcing.server.domain.entity.TaskCheck;
import com.campuscrowdsourcing.server.domain.entity.User;
import com.campuscrowdsourcing.server.domain.model.AjaxResult;
import com.campuscrowdsourcing.server.service.TaskCheckService;
import com.campuscrowdsourcing.server.service.TaskService;
import com.campuscrowdsourcing.server.utils.RequestUtil;
import org.apache.catalina.manager.util.SessionUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Xinyuan Zhao
 * @since 2023-03-14
 */
@RestController
@RequestMapping("/api/taskCheck")
public class TaskCheckController {
    @Resource
    private TaskCheckService taskcheckService;

    @PostMapping
    @Log(title = "新增我的审核", action = Action.INSERT)
    public AjaxResult saveTaskCheck(@RequestBody TaskCheck taskcheck) {
        taskcheckService.save(taskcheck);
        return AjaxResult.success();
    }

    @PutMapping
    @Log(title = "编辑我的审核", action = Action.UPDATE)
    public AjaxResult updateTaskCheck(@RequestBody TaskCheck taskcheck) {
        taskcheckService.updateById(taskcheck);
        List<TaskCheck> taskchecks = taskcheckService.list();
        for (TaskCheck taskcheck1 : taskchecks) {
            if(taskcheck1.getTaskId().equals(taskcheck.getTaskId()) && !taskcheck1.getUserId().equals(taskcheck.getUserId()))
            {
                taskcheckService.removeById(taskcheck1.getId());
            }
        }
        return AjaxResult.success();
    }

    @DeleteMapping("/{id}")
    @Log(title = "删除我的审核", action = Action.DELETE)
    public AjaxResult deleteTaskCheck(@PathVariable Integer id) {
        taskcheckService.removeById(id);
        return AjaxResult.success();
    }

    @PutMapping("/accept")
    @Log(title = "申请接受", action = Action.UPDATE)
    @LoginCheck
    public AjaxResult taskCheckAccept(@RequestParam Integer taskId,
                         @RequestParam Integer authorId,
                         @RequestParam(defaultValue = "") Integer teamId) {
        TaskCheck taskcheck = new TaskCheck();
        Integer userId = RequestUtil.getCurrentUserId();
        if(userId.equals(authorId))
        {
            return AjaxResult.fail("不能接受自己发布的任务");
        }
        taskcheck.setTaskId(taskId);
        taskcheck.setUserId(userId);
        taskcheck.setTeamId(teamId);
        taskcheckService.save(taskcheck);
        return AjaxResult.success();
    }

    @GetMapping
    @Log(title = "查看我的审核", action = Action.SELECT)
    public AjaxResult listTaskCheck() {
        return AjaxResult.success(taskcheckService.list());
    }

    @GetMapping("/{id}")
    @Log(title = "id查询我的审核", action = Action.SELECT)
    public AjaxResult getTaskCheckByid(@PathVariable Integer id) {
        TaskCheck taskCheck = taskcheckService.getById(id);
        if(taskCheck == null)
        {
            return AjaxResult.fail("该任务审核已删除或不存在");
        }
        return AjaxResult.success(taskCheck);
    }

    @GetMapping("/page")
    @Transactional
    @Log(title = "分页查询我的审核", action = Action.SELECT)
    public AjaxResult listTaskCheckPage(@RequestParam(defaultValue = "") String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<TaskCheck> queryWrapper = new QueryWrapper<TaskCheck>().orderByDesc("id");
        queryWrapper.like(!"".equals(name), "name", name);
        queryWrapper.eq("state",0);
        Integer loginUserId = RequestUtil.getCurrentUserId();
        queryWrapper.inSql("task_id", "select id from task where author = "+loginUserId.toString());
        return AjaxResult.success(taskcheckService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
}

